package com.encryption.service;

import com.encryption.EncryptionService;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 *
 */
public class Base64Service extends EncryptionService {


  @Override
  public String encryption(String param) {
    try {
      return this.encryptBASE64(param.getBytes());
    } catch (Exception e) {
      e.printStackTrace();
    }
    return "";
  }

  @Override
  public String decrypt(String param) {
    try {
      return new String(decryptBASE64(param));
    } catch (Exception e) {
      e.printStackTrace();
    }
    return "";
  }

  /**
   * BASE64解密
   */
  public byte[] decryptBASE64(String key) throws Exception {
    return (new BASE64Decoder()).decodeBuffer(key);
  }

  /**
   * BASE64加密
   */
  public String encryptBASE64(byte[] key) throws Exception {
    return (new BASE64Encoder()).encodeBuffer(key);
  }
}
